/****************************************************************
 * Fulguro
 * Copyright (C) 2004 Christophe Clienti
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 ***************************************************************/
#include <string.h>
#include "flgrCoreMalloc.h"
#include "flgrCoreData.h"
#include "flgrCoreTypes.h"
#include "flgrCoreDispatch.h"

#define FLGR_MACRO_PARSE_STR_CONSTANT(dtype,motif,var)	\
  for(k=0 ; k<spp ; k++) {				\
    sscanf(strin,motif,&var);				\
    *(((dtype*) (constout))+k) = (dtype) var;		\
    while((*strin!=' ') && (*strin!='\0')) strin++;	\
  }							\
  break

FLGR_Ret flgr_parse_str_constant(FLGR_Type type, int spp, char *strin, void *constout) {
  long long val_fixed;
  fgFLOAT64 val_float;
  int k;
  
  FLGR_DECLARE_FUNCTION;
  
  if(spp<0) {
    POST_ERROR("Size of vectors could not be < 1\n");
    return FLGR_RET_VECTOR_SIZE_ERROR;
  }

  switch(type) {
  case FLGR_BIT:
    FLGR_MACRO_PARSE_STR_CONSTANT(fgBIT,"%Ld",val_fixed);
  case FLGR_UINT8:
    FLGR_MACRO_PARSE_STR_CONSTANT(fgUINT8,"%Ld",val_fixed);
  case FLGR_UINT16:
    FLGR_MACRO_PARSE_STR_CONSTANT(fgUINT16,"%Ld",val_fixed);     
  case FLGR_UINT32:
    FLGR_MACRO_PARSE_STR_CONSTANT(fgUINT32,"%Ld",val_fixed);  
  case FLGR_UINT64:
    FLGR_MACRO_PARSE_STR_CONSTANT(fgUINT64,"%Ld",val_fixed); 
  case FLGR_INT8:
    FLGR_MACRO_PARSE_STR_CONSTANT(fgINT8,"%Ld",val_fixed);  
  case FLGR_INT16:
    FLGR_MACRO_PARSE_STR_CONSTANT(fgINT16,"%Ld",val_fixed);  
  case FLGR_INT32:
    FLGR_MACRO_PARSE_STR_CONSTANT(fgINT32,"%Ld",val_fixed);  
  case FLGR_INT64:
    FLGR_MACRO_PARSE_STR_CONSTANT(fgINT64,"%Ld",val_fixed);  
  case FLGR_FLOAT32:
    FLGR_MACRO_PARSE_STR_CONSTANT(fgFLOAT32,"%lf",val_float);
  case FLGR_FLOAT64:
    FLGR_MACRO_PARSE_STR_CONSTANT(fgFLOAT64,"%lf",val_float); 
  default:
    POST_ERROR("type unknown!\n");
    return FLGR_RET_TYPE_UNKNOWN;
  }

  return FLGR_RET_OK;

}


#define FLGR_MACRO_ALLOCATE_VECTOR_CONST(dtype)				\
  for(k=0 ; k<spp ; k++) {						\
    ((dtype *) tmp)[k] = *((dtype*) valueForEachVectorElement);		\
  }									\
  break

void *flgr_allocate_vector_const(FLGR_Type type, int spp, void *valueForEachVectorElement) {
  FLGR_Ret ret;
  void *tmp;
  int k;

  FLGR_DECLARE_FUNCTION;

  if( (ret=flgr_is_data_type_valid(type))!=FLGR_RET_OK) {
    POST_ERROR("unknown type\n");
    return NULL;
  }

  if(spp<0) {
    POST_ERROR("Size of vectors could not be < 1\n");
    return NULL;
  }

  tmp = flgr_malloc(flgr_get_sizeof(type) * spp);

  switch(type) {
  case FLGR_BIT:
    FLGR_MACRO_ALLOCATE_VECTOR_CONST(fgBIT);
  case FLGR_UINT8:
    FLGR_MACRO_ALLOCATE_VECTOR_CONST(fgUINT8);
  case FLGR_UINT16:
    FLGR_MACRO_ALLOCATE_VECTOR_CONST(fgUINT16);
  case FLGR_UINT32:
    FLGR_MACRO_ALLOCATE_VECTOR_CONST(fgUINT32);
  case FLGR_INT8:
    FLGR_MACRO_ALLOCATE_VECTOR_CONST(fgINT8);
  case FLGR_INT16:
    FLGR_MACRO_ALLOCATE_VECTOR_CONST(fgINT16);
  case FLGR_INT32:
    FLGR_MACRO_ALLOCATE_VECTOR_CONST(fgINT32);
  case FLGR_FLOAT32:
    FLGR_MACRO_ALLOCATE_VECTOR_CONST(fgFLOAT32);
  case FLGR_FLOAT64:
    FLGR_MACRO_ALLOCATE_VECTOR_CONST(fgFLOAT64);
  default:
    POST_ERROR("type unknown!\n");
    return NULL;
  }

  return tmp;
}
